package com.xmtlxx.web.controller.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.xmtlxx.oms.domain.*;
import com.xmtlxx.oms.mapper.*;
import com.xmtlxx.web.controller.center.service.ICommonService;
import com.xmtlxx.web.controller.wms.service.IExWarehouseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.entity.SysDept;
import com.xmtlxx.common.core.domain.entity.SysUser;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.enums.PropertyLogNode;
import com.xmtlxx.common.enums.PropertyLogType;
import com.xmtlxx.common.helper.LoginHelper;
import com.xmtlxx.common.tengbo.PushDatasUtil;
import com.xmtlxx.common.tengbo.vo.PropertyLogModelsVo;
import com.xmtlxx.common.utils.DateUtils;
import com.xmtlxx.common.utils.JsonUtils;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.common.utils.push.domain.PushContent;
import com.xmtlxx.oms.domain.vo.TaskVo;
import com.xmtlxx.system.domain.SysOss;
import com.xmtlxx.system.domain.bo.SysOssBo;
import com.xmtlxx.system.mapper.SysDeptMapper;
import com.xmtlxx.system.mapper.SysOssMapper;
import com.xmtlxx.system.mapper.SysUserMapper;
import com.xmtlxx.system.service.ISysNoticeService;
import com.xmtlxx.tms.domain.OrderDtl;
import com.xmtlxx.tms.domain.bo.OrderBo;
import com.xmtlxx.tms.domain.vo.OrderVo;
import com.xmtlxx.tms.mapper.OrderDtlMapper;
import com.xmtlxx.tms.mapper.OrderMapper;
import com.xmtlxx.tms.mapper.WaybillMapper;
import com.xmtlxx.wms.domain.ExWarehouse;
import com.xmtlxx.wms.domain.ExWarehouseDtl;
import com.xmtlxx.wms.domain.Inventory;
import com.xmtlxx.wms.domain.bo.ExWarehouseBo;
import com.xmtlxx.wms.domain.bo.ExWarehouseDtlBo;
import com.xmtlxx.wms.domain.bo.ExWarehouseProExportBo;
import com.xmtlxx.wms.domain.vo.ExWarehouseDtlVo;
import com.xmtlxx.wms.domain.vo.ExWarehouseProExportVo;
import com.xmtlxx.wms.domain.vo.ExWarehouseVo;
import com.xmtlxx.wms.domain.vo.StoreVo;
import com.xmtlxx.wms.mapper.ExWarehouseDtlMapper;
import com.xmtlxx.wms.mapper.ExWarehouseMapper;
import com.xmtlxx.wms.mapper.InventoryMapper;
import com.xmtlxx.wms.mapper.StoreMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 出库单Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class ExWarehouseServiceImpl implements IExWarehouseService {

    private final ExWarehouseMapper baseMapper;

    private final ExWarehouseDtlMapper exWarehouseDtlMapper;

    private final SysOssMapper sysOssMapper;

    private final OrderMapper orderMapper;

    private final OrderDtlMapper orderDtlMapper;

    private final TaskMapper taskMapper;

    private final TaskDtlMapper taskDtlMapper;

    private final OperatorLogMapper logMapper;

    private final StagingMapper stagingMapper;

    private final PropertyMapper propertyMapper;

    private final PropertyLogMapper propertyLogMapper;

    private final StoreMapper storeMapper;

    private final InventoryMapper inventoryMapper;

    private final ISysNoticeService sysNoticeService;

    private final SysUserMapper sysUserMapper;

    private final SysDeptMapper deptMapper;

    private final WaybillMapper waybillMapper;

    private final ICommonService commonService;

    /**
     * 查询出库单
     *
     * @param id 出库单主键
     * @return 出库单
     */
    @Override
    public ExWarehouseVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectExWarehouseVoByID(id);
    }

    @Override
    public ExWarehouseVo queryByTaskId(String taskId) {
        return baseMapper.selectExWarehouseVoByTaskId(taskId);
    }

    /**
     * 查询出库单列表
     *
     * @param bo 出库单
     * @return 出库单
     */
    @Override
    public TableDataInfo<ExWarehouseVo> queryPageList(ExWarehouseBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<ExWarehouse> lqw = buildQueryWrapper(bo);
       // Page<ExWarehouseVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<ExWarehouseVo> result = baseMapper.selectPageExWarehouseList(pageQuery.build(), bo);
        if(result != null && result.getRecords() != null && result.getRecords().size() > 0){
            for (ExWarehouseVo vo:result.getRecords()){
                SysOssBo ossBo = new SysOssBo();
                ossBo.setObjid(vo.getId());
                if(vo.getType().equals("1")){
                    ossBo.setBuss("wms_exWarehouse");
                }else {
                    ossBo.setBuss("wms_transfer");
                }
                List<SysOss> oss = sysOssMapper.selectSysOssList(ossBo);
                vo.setOss(oss);
            }
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询出库单列表
     *
     * @param bo 出库单
     * @return 出库单
     */
    @Override
    public List<ExWarehouseVo> queryList(ExWarehouseBo bo) {
       // LambdaQueryWrapper<ExWarehouse> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectExWarehouseList(bo);
    }

    private LambdaQueryWrapper<ExWarehouse> buildQueryWrapper(ExWarehouseBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ExWarehouse> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTaskId()), ExWarehouse::getTaskId, bo.getTaskId());
        lqw.eq(StringUtils.isNotBlank(bo.getExno()), ExWarehouse::getExno, bo.getExno());
        lqw.eq(bo.getDeptId() != null, ExWarehouse::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getStoreId()), ExWarehouse::getStoreId, bo.getStoreId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), ExWarehouse::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getReason()), ExWarehouse::getReason, bo.getReason());
        lqw.eq(bo.getTargetId() != null, ExWarehouse::getTargetId, bo.getTargetId());
        lqw.eq(StringUtils.isNotBlank(bo.getContacts()), ExWarehouse::getContacts, bo.getContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getHandId()), ExWarehouse::getHandId, bo.getHandId());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            ExWarehouse::getCreateTime ,params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), ExWarehouse::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getUpdateTime() != null, ExWarehouse::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), ExWarehouse::getUpdateBy, bo.getUpdateBy());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), ExWarehouse::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增出库单
     *
     * @param bo 出库单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ExWarehouse insertByBo(ExWarehouseBo bo) {
        ExWarehouse add = BeanUtil.toBean(bo, ExWarehouse.class);
        validEntityBeforeSave(add);

        String content = null;
        String logType = null;

        /**
         * 默认
         */
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");
        add.setIsSign("0");
        add.setIsFinish("0");
        add.setIsValidate("0");

        /**
         * 出库类型
         * 1-出库 2-处置 3-调用
         */

        switch (add.getType()){
            case "1":
                content = PropertyLogType.EX_WAREHOUSE.getInfo();
                logType = PropertyLogType.EX_WAREHOUSE.getCode();
                break;
            case "2":
                content = PropertyLogType.DEAL.getInfo();
                logType = PropertyLogType.DEAL.getCode();
                break;
            case "3":
                content = PropertyLogType.TRANSFER.getInfo();
                logType = PropertyLogType.TRANSFER.getCode();
                add.setReturnStatus("0");//归还状态
                break;
        }

        List<SysOss> ossList = new ArrayList<>();
        if(bo.getOss() != null && bo.getOss().size() > 0){
            for(SysOss oss : bo.getOss()){
                //是否下发的图片
                if(StringUtils.isNotEmpty(add.getTaskId())){
                    SysOss copyOss = new SysOss();
                    BeanUtil.copyProperties(oss,copyOss,"ossId");
                    if(add.getType().equals("2")){
                        copyOss.setBuss("wms_transfer");
                    }else {
                        copyOss.setBuss("wms_exWarehouse");
                    }
                    copyOss.setObjid(add.getId());
                    sysOssMapper.insert(copyOss);
                    ossList.add(copyOss);
                }else {
                    if(add.getType().equals("2")){
                        oss.setBuss("wms_transfer");
                    }else {
                        oss.setBuss("wms_exWarehouse");
                    }
                    oss.setObjid(add.getId());
                    sysOssMapper.updateById(oss);
                    ossList.add(oss);
                }
            }
        }

        List<ExWarehouseDtl> dtls = new ArrayList<>();
        if(bo.getItems() != null && bo.getItems().size() > 0){
            String finalLogType = logType;
            dtls = bo.getItems().stream().map(item->{
                ExWarehouseDtl dtl = BeanUtil.toBean(item,ExWarehouseDtl.class);
                dtl.setId(IdUtil.simpleUUID());
                dtl.setWid(add.getId());
                dtl.setDelFlag("0");
                dtl.setStatus("0");

                String logContent = finalLogType +"：(物品编号："+item.getPno()+"，物品名称："+item.getName()+")";
                /**
                 * 物品日志
                 */
//                commonService.savePropertyLog(dtl.getPno(),add.getId(),dtl.getName(),finalLogType,logContent,
//                    add.getDeptId(),JsonUtils.toJsonString(dtl),add.getStoreId(),dtl.getNums(), ossList);

                return dtl;
            }).collect(Collectors.toList());

            exWarehouseDtlMapper.insertBatch(dtls);
        }
        Long nums = dtls.stream().mapToLong(item-> item.getNums()).sum();
        add.setNums(nums);

        boolean flag = baseMapper.insert(add) > 0;

        /**
         * 操作日志
         */
//        commonService.saveOperatorLog(StringUtils.isNotEmpty(add.getTaskId())?add.getTaskId():add.getId(),"wms",content);
        bo.setId(add.getId());
        return add;
    }

    /**
     * 修改出库单
     *
     * @param bo 出库单
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ExWarehouse updateByBo(ExWarehouseBo bo) {
        ExWarehouse update = BeanUtil.toBean(bo, ExWarehouse.class);
        validEntityBeforeSave(update);

        Long nums = update.getNums();

        /**
         * 查询原明细数据
         */
        ExWarehouseDtlBo dtlBo = new ExWarehouseDtlBo();
        dtlBo.setWid(update.getId());
        List<ExWarehouseDtlVo> olds = exWarehouseDtlMapper.selectExWarehouseDtlList(dtlBo);

        List<ExWarehouseDtl> dtls = new ArrayList<>();

        if(bo.getItems() != null && bo.getItems().size() > 0){
            dtls = bo.getItems().stream().map(item->{
                ExWarehouseDtl dtl = new ExWarehouseDtl();
                BeanUtil.copyProperties(item,dtl);
                if(StringUtils.isEmpty(dtl.getId())){//修改后的物品
                    dtl.setId(IdUtil.simpleUUID());
                    dtl.setDelFlag("0");
                    dtl.setStatus("0");
                    dtl.setWid(update.getId());
                }
                /**
                 * 迭代查询移除的明细
                 */
                Iterator iterator = olds.iterator();
                while(iterator.hasNext()){
                    ExWarehouseDtlVo exWarehouseDtl = (ExWarehouseDtlVo) iterator.next();
                    if(exWarehouseDtl.getId().equals(dtl.getId())){
                        iterator.remove();
                    }
                }

                return dtl;
            }).collect(Collectors.toList());

            nums = dtls.stream().mapToLong(item-> item.getNums()).sum();
        }
        update.setNums(nums);

        if(dtls.size() > 0){
            exWarehouseDtlMapper.insertOrUpdateBatch(dtls);
        }

        //已删除的物品处理：删除dtl表、回退可移交状态
        if(olds != null && olds.size() > 0){
            olds.forEach(item -> {
                exWarehouseDtlMapper.deleteVoById(item.getId());
            });
        }
        if(bo.getOss() != null && bo.getOss().size() > 0){
            for(SysOss oss : bo.getOss()){
                //是否下发的图片
                if(StringUtils.isNotEmpty(update.getTaskId())){
                    SysOss copyOss = new SysOss();
                    BeanUtil.copyProperties(oss,copyOss,"ossId");
                    if(update.getType().equals("3")){
                        copyOss.setBuss("wms_transfer");
                    }else {
                        copyOss.setBuss("wms_exWarehouse");
                    }
                    copyOss.setObjid(update.getId());
                    sysOssMapper.insert(copyOss);
                }else {
                    if(update.getType().equals("3")){
                        oss.setBuss("wms_transfer");
                    }else {
                        oss.setBuss("wms_exWarehouse");
                    }
                    oss.setObjid(update.getId());
                    sysOssMapper.updateById(oss);
                }
            }
        }

        /**
         * isValidate 跨级确认
         * 0-无需确认 1-需确认 2-已确认
         */
        if(bo.getIsValidate().equals("2")){
            update.setValidateTime(new Date());//确认市级
            update.setValidater(LoginHelper.getUsername());//确认人
            /**
             * 操作日志
             */
//            commonService.saveOperatorLog(StringUtils.isNotEmpty(update.getTaskId())?update.getTaskId():update.getId(),"wms","区级确认出库单");

            //消息通知
            //消息接收人
            List<String> toUsers = new ArrayList<>();
            if(update.getDeptId() != null){
                //申请人
                SysUser sysUser = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getDeptId,update.getDeptId()));
                toUsers.add(sysUser.getUserId().toString());
            }


            //仓库
            TaskVo task = taskMapper.selectTaskVoByID(update.getId());
            if(task != null){
                StoreVo storeVo = storeMapper.selectVoById(task.getStoreId());
                if(storeVo != null){
                    SysUser storeUser = sysUserMapper.selectOne(new LambdaQueryWrapper<SysUser>().eq(SysUser::getDeptId,storeVo.getDeptId()));
                    if(storeUser != null){
                        toUsers.add(storeUser.getUserId().toString());
                    }
                }
            }
            if(toUsers != null && toUsers.size() > 0){
                String contentInfo = "您有1条出库单已确认，请及时查阅！";
                commonService.sendSysNotice("1",toUsers,update.getDeptId(),"出库单确认提醒",contentInfo,"{\"page\":\"/pagesOMS/staging/index\"}");
            }
        }
        baseMapper.updateById(update);
        return update;
    }

    @Override
    public Boolean updateExWarehouseByBo(ExWarehouseBo bo) {
        ExWarehouse update = BeanUtil.toBean(bo, ExWarehouse.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ExWarehouse entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除出库单
     *
     * @param ids 需要删除的出库单主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean returnWithValidByIds(Collection<String> ids, Boolean isValid) {
        List<ExWarehouse> exWarehouseList = new ArrayList<>();
        for (String id:ids){
            ExWarehouse exWarehouse = baseMapper.selectById(id);
            exWarehouse.setReturnStatus("1");
            exWarehouse.setReturnTime(new Date());
            exWarehouseList.add(exWarehouse);

            ExWarehouseDtlBo bo = new ExWarehouseDtlBo();
            bo.setWid(id);
            List<ExWarehouseDtlVo> dtls = exWarehouseDtlMapper.selectExWarehouseDtlList(bo);
            if(dtls != null && dtls.size() > 0){
                for (ExWarehouseDtlVo dtl:dtls){
                    Staging staging = stagingMapper.selectOne(new LambdaQueryWrapper<Staging>().eq(Staging::getPno,dtl.getPno()));
                    //移交登记
                    if(staging != null){
                        if(staging.getType().equals("1")){
                            staging.setReturnStatus("1");//设置归还状态
                            staging.setReturnTime(new Date());//设置归还时间
                            stagingMapper.updateById(staging);
                        }
                    }

                    Property property = propertyMapper.selectOne(new LambdaQueryWrapper<Property>().eq(Property::getPno,dtl.getPno()));
                    if(property != null){
                        property.setTransferType("2");//设置类型为暂缓移交
                        propertyMapper.updateById(property);
                    }
                }
            }
        }
        return baseMapper.updateBatchById(exWarehouseList);
    }

    @Override
    public Boolean returnTransfer(Collection<String> ids, Boolean isValid) {
        List<ExWarehouse> exWarehouseList = new ArrayList<>();
        boolean flag = false;
        for (String id:ids){
            ExWarehouse exWarehouse = baseMapper.selectById(id);
            exWarehouse.setReturnStatus("1");//设置归还状态
            exWarehouse.setReturnTime(new Date());
            exWarehouseList.add(exWarehouse);

            ExWarehouseDtlBo bo = new ExWarehouseDtlBo();
            bo.setWid(id);
            List<ExWarehouseDtlVo> dtls = exWarehouseDtlMapper.selectExWarehouseDtlList(bo);
            if(dtls != null && dtls.size() > 0){
                for (ExWarehouseDtlVo dtl:dtls){

                    Property property = propertyMapper.selectOne(new LambdaQueryWrapper<Property>().eq(Property::getPno,dtl.getPno()));
                    if(property != null){
                        property.setNums(property.getNums() + dtl.getNums());//库存减去调用数
                        property.setTnums(0l);//调用数清零
                        property.setRnums(dtl.getNums());//设置归还数
                        propertyMapper.updateById(property);
                    }

                    Inventory inventory = inventoryMapper.selectOne(new LambdaQueryWrapper<Inventory>().eq(Inventory::getPno,dtl.getPno()));
                    if(inventory != null){
                        inventory.setNums(inventory.getNums() + dtl.getNums());//库存减去调用数
                        inventory.setTnums(0l);//调用数清零
                        inventory.setRnums(dtl.getNums());//设置归还数
                        inventoryMapper.updateById(inventory);
                    }
                }
            }
            flag = baseMapper.updateBatchById(exWarehouseList);

            //更新task状态
            Task task = taskMapper.selectById(exWarehouse.getTaskId());
            if(ObjectUtil.isNotNull(task)){
                task.setStatus("1");
                taskMapper.updateById(task);
            }
            //消息通知
            //消息接收人
            List<String> toUsers = new ArrayList<>();
            if(exWarehouse.getDeptId() != null){
                //申请人
                SysUser userBo = new SysUser();
                userBo.setDeptId(exWarehouse.getDeptId());
                List<SysUser> sysUsers = sysUserMapper.selectUsers(userBo);
//                if(sysUser != null){
//                    toUsers.add(sysUser.getUserId().toString());
//                }
                if(sysUsers != null && sysUsers.size() > 0){
                    toUsers.add(sysUsers.get(0).getUserId().toString());
                }
            }
            //仓库
            StoreVo storeVo = storeMapper.selectVoById(task.getStoreId());
            if(storeVo != null){
                toUsers.add(storeVo.getUserId().toString());
            }
            if(toUsers != null && toUsers.size() > 0){
                PushContent pushContent = new PushContent();
                pushContent.setTitle("调用归还提醒");
                pushContent.setPayload("{\"page\":\"/pagesOMS/staging/index\"}");
                pushContent.setClickType("payload");
//                String contentInfo = "您有"+(bo.getNums() != null ? bo.getNums() :"1")+(StringUtils.isNotBlank(bo.getUnit())?bo.getUnit():"件")+" "+bo.getName()+"待接收登记，请及时处理！";
                String contentInfo = "您有1条调用单已归还，请及时查阅！";
                pushContent.setContent(contentInfo);
                pushContent.setBody(pushContent.getContent());
                sysNoticeService.sendNotice("1",toUsers,exWarehouse.getDeptId(),pushContent,contentInfo);
            }
        }
        return flag;
    }

    public String getNo(){
        LambdaQueryWrapper<ExWarehouse> lqw = Wrappers.lambdaQuery();
        Long maxNumObject = baseMapper.selectCount(lqw);
        String num = "";
        String todayStr = DateUtils.date2String(new Date(), "yyMMdd");

        if (maxNumObject == null) {
            num = todayStr + "000001";
        } else {
            String maxnumStr = "00000" + maxNumObject.toString();
            String maxnumNo = maxnumStr.substring(maxnumStr.length() - 6, maxnumStr.length());
            String nextnumNo = StringUtils.nextNo(maxnumNo, 6);
            num = todayStr + nextnumNo;
        }
        return num;
    }

    public void updateOrder(String taskId,List<ExWarehouseDtlVo> items){
        OrderBo orderBo = new OrderBo();
        orderBo.setTaskId(taskId);
        List<OrderVo> orderVos = orderMapper.selectOrderList(orderBo);
        List<OrderDtl> dtls = new ArrayList<>();
        if(orderVos != null && orderVos.size() > 0){
            OrderVo vo = orderVos.get(0);
            for (ExWarehouseDtlVo item:items){
                OrderDtl dtl = BeanUtil.toBean(item,OrderDtl.class);

                LambdaQueryWrapper<OrderDtl> lqw = Wrappers.lambdaQuery();
                lqw.eq(StringUtils.isNotBlank(vo.getId()), OrderDtl::getOrderId, vo.getId());
                lqw.eq(StringUtils.isNotBlank(item.getPno()), OrderDtl::getPno, item.getPno());
                OrderDtl orderDtl = orderDtlMapper.selectOne(lqw);

                if(StringUtils.isEmpty(item.getId()) && orderDtl == null){
                    dtl.setId(IdUtil.simpleUUID());
                    dtl.setOrderId(vo.getId());
                    dtl.setDelFlag("0");
                    dtl.setStatus("0");
                }else {
                    dtl.setId(orderDtl.getId());
                }
                dtls.add(dtl);
            }
        }
        if(dtls != null && dtls.size() > 0){
            orderDtlMapper.insertOrUpdateBatch(dtls);
        }
    }

    public void updateTask(String taskId,List<ExWarehouseDtlVo> items){
        TaskVo taskVo = taskMapper.selectTaskVoByID(taskId);
        List<TaskDtl> dtls = new ArrayList<>();
        if(taskVo != null){
            for (ExWarehouseDtlVo item:items){
                TaskDtl dtl = BeanUtil.toBean(item,TaskDtl.class);

                LambdaQueryWrapper<TaskDtl> lqw = Wrappers.lambdaQuery();
                lqw.eq(StringUtils.isNotBlank(taskVo.getId()), TaskDtl::getTaskId, taskVo.getId());
                lqw.eq(StringUtils.isNotBlank(item.getPno()), TaskDtl::getPno, item.getPno());
                TaskDtl taskDtl = taskDtlMapper.selectOne(lqw);

                if(StringUtils.isEmpty(item.getId()) && taskDtl == null){
                    dtl.setId(IdUtil.simpleUUID());
                    dtl.setTaskId(taskVo.getId());
                    dtl.setDelFlag("0");
                }else {
                    dtl.setId(taskDtl.getId());
                }
                dtls.add(dtl);
            }
        }
        if(dtls != null && dtls.size() > 0){
            taskDtlMapper.insertOrUpdateBatch(dtls);
        }
    }

    //日志添加
    public void  addOperatorLog(String objid,String type,String content){
        OperatorLog log = new OperatorLog();
        log.setId(IdUtil.simpleUUID());
        log.setObjid(objid);
        log.setDelFlag("0");
        log.setLogTime(new Date());
        log.setUserId(LoginHelper.getUserId());
        log.setOperator(LoginHelper.getNickname());
        log.setLogType(type);
        log.setContent(content);
        logMapper.insert(log);
    }

    //财物日志添加
    private  void  saveLog(ExWarehouseDtl dtl,String objid, Long deptId,String storeId, String content,List<SysOss> osses){
        PropertyLog propertyLog = new PropertyLog();

        BeanUtil.copyProperties(dtl,propertyLog,"id");
        propertyLog.setId(IdUtil.simpleUUID());
        propertyLog.setObjid(objid);
        propertyLog.setLogTime(new Date());
        propertyLog.setLogType(PropertyLogType.EX_WAREHOUSE.getCode());//0-移交登记 1-入库 2-出库 3-调用 4-处置 5-交接 6-上架 7-挑拣
        propertyLog.setContent(content);
        propertyLog.setReceiveDept(deptId);
        propertyLog.setUserId(LoginHelper.getUserId());
        propertyLog.setOperator(LoginHelper.getNickname());
        propertyLog.setInfo(JsonUtils.toJsonString(dtl));
        propertyLog.setDelFlag("0");
        propertyLog.setStoreId(storeId);

        SysDept dept = deptMapper.selectById(LoginHelper.getDeptId());
        propertyLog.setNode(PropertyLogNode.getNodeCodeByBuss(dept.getLevel(),"1"));
        int i =  propertyLogMapper.insert(propertyLog);
        if(osses != null && osses.size() > 0){
            for(SysOss oss : osses){
                SysOss copyOss = new SysOss();
                BeanUtil.copyProperties(oss,copyOss,"ossId");
                copyOss.setObjid(propertyLog.getId());
                copyOss.setBuss("propertyLog");
                sysOssMapper.insert(copyOss);
            }
        }
        if(i > 0){
            PropertyLogModelsVo data = new PropertyLogModelsVo();
            data.setApi("proBase");
            data.setData(propertyLogMapper.selectVoById(propertyLog.getId()));
            PushDatasUtil.pushPropertyDatas(data);
        }
    }

    @Override
    public List<ExWarehouseProExportVo> getExWarehouseProExportList(ExWarehouseProExportBo bo) {
        return baseMapper.getExWarehouseProExportList(bo);
    }
}
